package ms.maplestory.boot.world.field.objects.mob;

import lombok.Getter;
import lombok.Setter;
import ms.maplestory.boot.client.character.skills.Mask;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ScheduledFuture;

@Getter
@Setter
public class MobTemporaryStat {

    private Mob mob;
    private Comparator<MobStat> mobStatComparator = (o1, o2) -> {
        int res = 0;
//        if (o1.getPos() < o2.getPos()) {
//            res = -1;
//        } else if (o1.getPos() > o2.getPos()) {
//            res = 1;
//        } else {
//            if (o1.getValue() < o2.getValue()) {
//                res = -1;
//            } else if (o1.getValue() > o2.getValue()) {
//                res = 1;
//            }
//        }
        return res;
    };
    private TreeMap<MobStat, Mask> newStatValues = new TreeMap<>(mobStatComparator);
    private final TreeMap<MobStat, Mask> currentStatValues = new TreeMap<>(mobStatComparator);
    private TreeMap<MobStat, Mask> removedStatValues = new TreeMap<>(mobStatComparator);
    private Map<MobStat, ScheduledFuture> schedules = new HashMap<>();

    public MobTemporaryStat(Mob mob) {
        this.mob = mob;
    }

    public void addStatMask(MobStat mobStat, Mask mask) {
//        mask.tTerm *= 1000;
//        mask.tOption *= 1000;
//        int tAct = mask.tOption > 0 ? mask.tOption : mask.tTerm;
        newStatValues.put(mobStat, mask);
        currentStatValues.put(mobStat, mask);
//        if (tAct > 0 && mobStat != BurnedInfo) {
//            if (getSchedules().containsKey(mobStat)) {
//                getSchedules().get(mobStat).cancel(true);
//            }
//            ScheduledFuture sf = EventManager.addEvent(() -> removeMobStat(mobStat, true), tAct);
//            getSchedules().put(mobStat, sf);
//        }
    }

    public void removeMobStat(MobStat mobStat, boolean fromSchedule) {
        synchronized (currentStatValues) {
            removedStatValues.put(mobStat, currentStatValues.get(mobStat));
            currentStatValues.remove(mobStat);
            //mob.getField().broadcastPacket(MobPool.statReset(getMob(), (byte) 1, false));
            getSchedules().remove(mobStat);
            if (!fromSchedule && getSchedules().containsKey(mobStat)) {
                getSchedules().get(mobStat).cancel(true);
            }
            getSchedules().remove(mobStat);
        }
    }

    public Map<MobStat, ScheduledFuture> getSchedules() {
        if (schedules == null) {
            schedules = new HashMap<>();
        }
        return schedules;
    }
}
